package com.ly.util;

import com.ly.entity.Student;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 李岩
 * @date 2021/12/16-13:42
 * @describe
 */
public class JedisUtil {

    //redis的Java客户端对象 //单个节点
    private Jedis jedis;
    //redis的Java客户端对象 //集群
    private JedisCluster jedisCluster;

    {
        jedis=new Jedis("127.0.0.1",6379);
        HostAndPort hostAndPort6382=new HostAndPort("127.0.0.1",6382);
        HostAndPort hostAndPort6383=new HostAndPort("127.0.0.1",6383);
        HostAndPort hostAndPort6384=new HostAndPort("127.0.0.1",6384);
        Set<HostAndPort> set = new HashSet<HostAndPort>();
        set.add(hostAndPort6382);
        set.add(hostAndPort6383);
        set.add(hostAndPort6384);
        jedisCluster=new JedisCluster(set);
    }

    /*//获取学生id
    public String getSid(){
        if (jedisCluster.get("id")==null){
            jedisCluster.set("id","1");
            return jedisCluster.get("id");
        }
        jedisCluster.incr("id");
        return jedisCluster.get("id");
    }
    //把学生信息放到redis的string里
    public Student setStu(Student stu){
        String stuid=this.getSid();
        stu.setStuid(Integer.valueOf(stuid));
        if (stu!=null){
            jedisCluster.set("stu_"+stuid,stu.toString());
        }
        jedisCluster.lpush("stu_id_all",stuid);
        return stu;
    }
    //展示所有学生信息
    public List<String> findAllStudent(){
        List<String> list = new ArrayList<>();
        for (String key : jedisCluster.lrange("stu_id_all",0,-1)) {
            String s = jedisCluster.get(key);
            list.add(s);
        }
        return list;
    }

    //把学号放到redis的set里
    public void setclass(String name,Student student){
        jedisCluster.sadd("cla_"+name,student.toString());
    }

    //展示所有学生信息 在 set无序
    public List<String> findAllStudentBySet(String name){
        List<String> list = new ArrayList<>();
        for (String key : jedisCluster.smembers("cla_"+name)) {
            list.add(key);
        }
        return list;
    }
*/
    //获取学生id
    public String getSid(){
        if (jedis.get("id")==null){
            jedis.set("id","1");
            return jedis.get("id");
        }
        jedis.incr("id");
        return jedis.get("id");
    }
    //把学生信息放到redis的string里
    public Student setStu(Student stu){
        String stuid=this.getSid();
        stu.setStuid(Integer.valueOf(stuid));
        if (stu!=null){
            jedis.set("stu2_"+stuid,stu.toString());
        }
        return stu;
    }
    //展示所有学生信息
    public List<String> findAllStudent(){
        List<String> list = new ArrayList<>();
        for (String key : jedis.keys("stu2_*")) {
            String s = jedis.get(key);
            list.add(s);
        }
        return list;
    }

    //把学号放到redis的set里
    public void setclass(String name,Student student){
       jedis.sadd("cla_"+name,student.toString());
    }

    //展示所有学生信息 在 set无序
    public List<String> findAllStudentBySet(String name){
        List<String> list = new ArrayList<>();
        for (String key : jedis.smembers("cla_"+name)) {
            list.add(key);
        }
        return list;
    }

}
